<?php

namespace app\admin\controller;

use app\common\model\Domains;
use app\common\model\Quns;
use app\common\model\Users;
use app\common\model\Versions;
use think\facade\Env;
use think\Request;
use think\Db;

class Index extends Base
{

    public function index()
    {
        $domain_num_true = Domains::where('status', 1)->count('id');
        $domain_num = Domains::count('id');
        $this->assign(['domain_num_true'=>$domain_num_true, 'domain_num'=>$domain_num]);
        return view();
    }


    public function home(Request $request)
    {
        /* 统计 碎片 */
        $tj = [
            'domain_num'      => Domains::count('id'),
            'domain_num_true' => Domains::where('status', 1)->count('id'),
            'rukou_num'       => Domains::where('type', 1)->count('id'),
            'rukou_num_true'  => Domains::where(['status'=>1, 'type'=>1])->count('id'),
            'luodi_num'       => Domains::where('type', 2)->count('id'),
            'luodi_num_true'  => Domains::where(['status'=>1, 'type'=>2])->count('id'),
            'qun_num'         => Quns::count('id'),
        ];
        $this->assign('tj', $tj);

        return view();
    }


	public function jm()
	{
		return view();
	}


    /* 修改密码 */
    public function password(Request $request)
    {
        if ( $request->isPost() ){

            $post = trimAll($request->post());
            $username = session('admin.username');
            $user = Users::where('username', $username)->find();

            if ( $user['password'] !== sha1_password($post['oldpwd']) ){
                error('旧密码输入错误');
            }

            if ( $post['newpwd'] !== $post['confirmpwd'] ){
                error('两次输入的新密码必须相同');
            }

            if ( Users::where('id', $user['id'])->update(['password'=>sha1_password($post['newpwd'])]) ){
                createLog('修改密码成功：' . $user['username']);
                session('admin', null);
                session(null);
                success('修改密码成功');
            } else {
                createLog('修改密码失败：' . $user['username'] . ' / ' . $post['newpwd']);
                error('修改密码失败');
            }

        }
        return view();
    }


}